package com.epin.business.system.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.epin.base.exception.check.RecordQuoteException;
import com.epin.base.exception.check.RecordRepeatException;
import com.epin.base.plugins.AbstractBean;
import com.epin.base.plugins.BaseController;
import com.epin.business.system.entity.Dept;
import com.epin.business.system.entity.vo.DeptParam;
import com.epin.business.system.service.DeptService;

/**
 * <p>
 * 群组管理  前端控制器
 * </p>
 *
 * @author laijianbin
 * @since 2017-06-05
 */
@Controller
@RequestMapping("/dept/")
public class DeptController extends BaseController {

	@Autowired
	private DeptService deptService;
	
	/**
	 * @Title: listUI
	 *
	 * @Description：列表UI访问
	 * 
	 * @param 
	 *
	 * @return String
	 *
	 */
	@RequestMapping(value = "listUI", method = RequestMethod.GET)
    public String listUI() {
		return "business/system/dept/list";
    }
	
	
	/**
	 * @Title: form
	 *
	 * @Description：表格访问
	 * 
	 * @param map
	 *
	 * @return String
	 *
	 */
	@RequestMapping(value = "form")
    public String form(Map<String, Object> map) {
		return "business/system/dept/form";
    }
	
	
	/**
	 * @Title: select
	 *
	 * @Description：按id查找访问
	 * 
	 * @param map, id
	 *
	 * @return String
	 *
	 */
	@RequestMapping(value = "{id}/select")
    public String select(Map<String, Object> map, @PathVariable(required = true) Integer id) {	
		
		Dept dept = deptService.selectById(id);
		
		map.put("dept", dept);
		
		return "business/system/dept/edit";
    }
    
    
	/**
	 * @Title: load
	 *
	 * @Description：列表数据访问
	 * 
	 * @param pageNumber，pageSize，keyword
	 *
	 * @return Object
	 *
	 */
    @RequestMapping(value = "load")
	@ResponseBody
    public Object load(int pageNumber, int pageSize, String keyword) {
    	
    	pageNumber = pageNumber <= 0 ? 1 : pageNumber;
    	pageSize = pageSize <= 0 ? 10 : pageSize;
    	
    	Map<String, Object> params = getParams();
    	
    	params.put("keyword", keyword);
    	
    	// 获取分页
    	Page<Dept> result = deptService.selectDeptsPage(pageNumber, pageSize, params);
    	
    	Map<String, Object> pageResponse = new HashMap<String, Object>();
    	
    	pageResponse.put("total", result.getTotal());
		pageResponse.put("rows", result.getRecords());
		
		return pageResponse;
    }
	
	
    /**
	 * @Title: save
	 *
	 * @Description：添加或修改访问
	 * 
	 * @param id，dParam
	 *
	 * @return AbstractBean
	 *
	 */
	@RequestMapping(value = "save")
	@ResponseBody
	public AbstractBean save(Integer id, DeptParam dParam){
		
		try {
			return result(deptService.insertOrUpdateDept(id, dParam, getOperator()));
		} catch (RecordRepeatException e) {
			return fail(e.getMessage());
		}
		
	}
	
	
	/**
	 * @Title: delete
	 *
	 * @Description：删除访问
	 * 
	 * @param ids
	 *
	 * @return AbstractBean
	 * 
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
    public AbstractBean delete(Integer[] ids) {
		
		try {
			return result(deptService.deleteDeptsByIds(Arrays.asList(ids)));
		} catch (RecordQuoteException e) {
			return fail(e.getMessage());
		}
		
    }
	
	
}
